package com.baibei.pay.selector;

import android.content.res.AssetManager;

import com.baibei.model.CityInfo;
import com.baibei.model.ProvinceInfo;
import com.blankj.utilcode.utils.CloseUtils;
import com.blankj.utilcode.utils.LogUtils;
import com.blankj.utilcode.utils.Utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * 帮助类
 * Created by shenminjie on 2017/8/22.
 * mail:shenminjie@100bei.com
 */

public class ProvinceHelper {

    /**
     * 获取城市列表
     *
     * @return
     */
    public static List<ProvinceInfo> getCityInfos() {
        AssetManager assetManager = Utils.getContext().getAssets();
        Scanner in;
        InputStream inputStream = null;
        List<CityInfo> cityInfos = new ArrayList<>();
        //先获取所有的省市
        List<String> provincesNames = new ArrayList<>();
        try {
            inputStream = assetManager.open("province_city.csv");
            in = new Scanner(inputStream, "GBK");
            while (in.hasNextLine()) {
                String[] lines = in.nextLine().split(",");
                String province = lines[0];
                String city = lines[1];
                CityInfo cityInfo = new CityInfo();
                cityInfo.setProvince(province);
                cityInfo.setName(city);
                cityInfos.add(cityInfo);
                //保存省市
                if (!provincesNames.contains(province)) {
                    provincesNames.add(province);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            CloseUtils.closeIO(inputStream);
        }
        List<ProvinceInfo> provinceInfos = new ArrayList<>();
        for (String provinceName : provincesNames) {
            ProvinceInfo provinceInfo = new ProvinceInfo();
            provinceInfo.setName(provinceName);
            List<CityInfo> cityInfo2 = new ArrayList<>();
            for (CityInfo cityInfo : cityInfos) {
                if (cityInfo.getProvince().equals(provinceName)) {
                    cityInfo2.add(cityInfo);
                }
            }
            provinceInfo.setCityInfos(cityInfo2);
            provinceInfos.add(provinceInfo);
        }
        LogUtils.e(provinceInfos);
        return provinceInfos;
    }


}
